/*
 * 题目描述：给出选手的信息，然后进行多个字段不同优先级进行排序
 *
 * 坑：题目测试了一种分数全为0的，需要注意数组初始值不能为默认的0
 */
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

struct Testee{
    string regNum;
    int locationNum;
    int score;
    int local_rank;
    int final_rank;
};
int N,K,total;
vector<Testee> testees;

bool cmpByRegNum(Testee &t1, Testee &t2){
    return t1.regNum < t2.regNum;
}

bool cmpByScore(const Testee &t1, const Testee &t2){
    return t1.score > t2.score;
}

int main() {
    cin >> N;
    int *ranks = new int[N]();
    int *scores = new int[N]();
    for (int i = 0; i < N; i++) {
        scores[i] = -1;
    }
    int *num = new int[N]();
    int location = 0;
    string regNum;
    int score;
    while (N--) {
        cin >> K;
        total += K;
        location++;
        while(K--){
            cin >> regNum >> score;
            Testee t;
            t.locationNum = location;
            t.regNum = regNum;
            t.score = score;
            testees.push_back(t);
        }
    }
    sort(testees.begin(), testees.end(),cmpByRegNum);
    stable_sort(testees.begin(), testees.end(), cmpByScore);

    int lastScore = -1;
    int final_rank = 0;
    for (int i = 0; i < testees.size(); i++) {
        num[testees[i].locationNum - 1]++;
        if (scores[testees[i].locationNum - 1] == testees[i].score) {
            testees[i].local_rank = ranks[testees[i].locationNum - 1];
        }else{
            scores[testees[i].locationNum - 1] = testees[i].score;
            testees[i].local_rank = ranks[testees[i].locationNum - 1] = num[testees[i].locationNum - 1];
        }
        if (lastScore == testees[i].score) {
            testees[i].final_rank = final_rank;
        }else{
            lastScore = testees[i].score;
            testees[i].final_rank = final_rank = i + 1;
        }
    }
    printf("%d\n", total);
    for (int i = 0; i < testees.size(); i++) {
        cout << testees[i].regNum << " "
             << testees[i].final_rank << " "
             << testees[i].locationNum << " "
             << testees[i].local_rank << endl;
    }
    return 0;
}